package com.project.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.framework.core.http.HttpRequest;
import com.framework.core.http.HttpResponse;
import com.framework.core.http.HttpUtil;
import com.framework.core.text.MD5Util;
import com.project.pojo.model.QueryTrackParam;
import com.project.pojo.model.QueryTrackReq;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 快递100帮助类
 * @Author: zhuchi
 * @CreateDate: 2020/12/11 18:15
 * @Version: 1.0
 */
@Slf4j
public class Kuaidi100Util {

    private final static String CHARSET_DEFAULT = "UTF-8";

    /**
     * 根据物流公司代码和物流单号查询物流信息
     * @param companyCode
     * @param logisticsNumber
     * @return
     */
    public static JSONObject logisticsQuery(String companyCode, String logisticsNumber){
        String key = "HpzRnojI6476";
        String customer = "096C362DC2F3E62CA7A5AFD8FE87BA7C";

        HttpRequest post = HttpUtil.createPost("https://poll.kuaidi100.com/poll/query.do");
        QueryTrackParam queryTrackParam = new QueryTrackParam();
        queryTrackParam.setCom(companyCode);
        queryTrackParam.setNum(logisticsNumber);

        String param = JSONObject.toJSONString(queryTrackParam);

        String sign = MD5Util.getMD5(param + key + customer);
        sign = sign.toUpperCase();

        QueryTrackReq queryTrackReq = new QueryTrackReq();
        queryTrackReq.setParam(param);
        queryTrackReq.setCustomer(customer);
        queryTrackReq.setSign(sign);

        post.charset("UTF-8");
        JSONObject jsonObject = doPost("https://poll.kuaidi100.com/poll/query.do", queryTrackReq);
        log.info("根据物流单号[{}]查询物流信息，返回数据：{}", logisticsNumber, jsonObject);
        System.out.println("根据物流单号["+logisticsNumber+"]查询物流,返回数据：" + jsonObject);

        return jsonObject;
    }

    public static void main(String[] args) {
        logisticsQuery("huitongkuaidi", "557030694012281");
    }

    /**
     * post请求  编码格式默认UTF-8
     * @param url     请求url
     * @return
     */
    public static JSONObject doPost(String url,  Object obj) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse resp = null;

        JSONObject result = null;
        try {
            Map<String, String> params = ObjectToMapUtils.objectToMap(obj);
            HttpPost httpPost = new HttpPost(url);
            if (params != null && params.size() > 0) {
                List<NameValuePair> list = new ArrayList<NameValuePair>();
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
                httpPost.setEntity(new UrlEncodedFormEntity(list, CHARSET_DEFAULT));
            }

            resp = httpClient.execute(httpPost);
            String body = EntityUtils.toString(resp.getEntity(), CHARSET_DEFAULT);
            int statusCode = resp.getStatusLine().getStatusCode();
            if (statusCode == 200){
                result = JSONObject.parseObject(body);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != resp) {
                try {
                    resp.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

}
